﻿#include "sqlitedpool.h"

SQLitedPool::SQLitedPool(QObject *parent)
    : QObject{parent}
{
    reset_LG_DataBase();
    reset_PL_DataBase();
    reset_QR_DataBase();
}

SQLitedPool *SQLitedPool::getInstance()
{
    static SQLitedPool instance;
    return &instance;
}

void SQLitedPool::reset_LG_DataBase()
{
    if(m_LG_Choose)
    {
        m_LG_Choose->close();

        m_LG_Choose->removeDatabase("LG_ChooseDB");

        delete m_LG_Choose;

        m_LG_Choose = nullptr;
    }

    m_LG_Choose = new QSqlDatabase();

    *m_LG_Choose = QSqlDatabase::addDatabase("QSQLITE",QString("LG_Choose"));

    m_LG_Choose->setDatabaseName("./LG_ChooseParameter.db");
    if(!m_LG_Choose->open())
    {
        qDebug()<<"Cpp:m_LG_Choose->lastError():"<<m_LG_Choose->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : LG_ChooseParameter.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  LG_ChooseParameter 成功";
}

void SQLitedPool::reset_PL_DataBase()
{
    if(m_PL_Process)
    {
        m_PL_Process->close();

        m_PL_Process->removeDatabase("PL_ProcessDB");

        delete m_PL_Process;

        m_PL_Process = nullptr;
    }

    m_PL_Process = new QSqlDatabase();

    *m_PL_Process = QSqlDatabase::addDatabase("QSQLITE",QString("PL_Process"));

    m_PL_Process->setDatabaseName("./PL_ProcessParameter.db");
    if(!m_PL_Process->open())
    {
        qDebug()<<"Cpp:m_PL_Process->lastError():"<<m_PL_Process->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : PL_ProcessParameter.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  PL_ProcessParameter 成功";
}

void SQLitedPool::reset_QR_DataBase()
{
    if(m_QR_Process)
    {
        m_QR_Process->close();

        m_QR_Process->removeDatabase("QR_ProcessDB");

        delete m_QR_Process;

        m_QR_Process = nullptr;
    }

    m_QR_Process = new QSqlDatabase();

    *m_QR_Process = QSqlDatabase::addDatabase("QSQLITE",QString("QR_Process"));

    m_QR_Process->setDatabaseName("./QR_ProcessParameter.db");
    if(!m_QR_Process->open())
    {
        qDebug()<<"Cpp:m_QR_Process->lastError():"<<m_QR_Process->lastError().text().toUtf8().data();
    }
    else
    {
        qDebug()<<"Cpp:连接数据库 : QR_ProcessParameter.db 成功";
    }

    qDebug()<<"Cpp: 重置数据库  QR_ProcessParameter 成功";
}

QSqlDatabase *SQLitedPool::get_LG_DataBase()
{
    return m_LG_Choose;
}

QSqlDatabase *SQLitedPool::get_PL_DataBase()
{
    return m_PL_Process;
}

QSqlDatabase *SQLitedPool::get_QR_DataBase()
{
    return m_QR_Process;
}
